package com.ckzp.core.util;

import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by 51594 on 2022/7/28.
 */
public class ImageUtil {


    /**
     * 检查是否指定宽高
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static boolean checkImageWidthHeight(String path, int width, int height) {
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(path));
            int widths = bufferedImage.getWidth();
            int heights = bufferedImage.getHeight();
            if (width == widths && height == heights) {
                return true;
            }
        } catch (IOException e) {
            //e.printStackTrace();
        }
        return false;
    }


    /**
     * 检查是否指定宽高
     *
     * @param path
     * @param size
     * @return
     */
    public static boolean checkImageSize(String path, long size) {
        try {
            File file = new File(path);
            if (file.length() < size) {
                return true;
            }
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return false;
    }

    /**
     * 取得图片宽高
     *
     * @param path
     * @return
     */
    public static String getImageWidthHeight(String path) {
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(path));
            int widths = bufferedImage.getWidth();
            int heights = bufferedImage.getHeight();
            return "[" + widths + "," + heights + "]";
        } catch (IOException e) {
            //e.printStackTrace();
        }
        return "[]";
    }

    /**
     * @param oldname
     * @return
     */
    public static String getFileRename(String oldname) {
        int index = oldname.lastIndexOf(".");
        String ext = oldname.substring(index, oldname.length());
        if (ext != null) ext = ext.toLowerCase();
        long filename = DateUtil.getDate().getTime();
        return filename + ext;
    }

    /**
     * @param oldname
     * @return
     */
    public static String getFileRename(String oldname, long newlongs) {
        int index = oldname.lastIndexOf(".");
        String ext = oldname.substring(index, oldname.length());
        if (ext.indexOf(".png") > -1) ext = ".png";
        if (ext.indexOf(".jpg") > -1) ext = ".jpg";
        if (ext.indexOf(".gif") > -1) ext = ".gif";
        if (ext != null) ext = ext.toLowerCase();
        return newlongs + ext;
    }

    /**
     * 保存base64图片
     *
     * @param basestr
     * @param savepath
     */
    public void saveBase64Image(String basestr, String savepath) {
        String ext = "data:image/jpg;base64";
        basestr = basestr.split(",")[1];//去掉base64码的data:image/png;base64,
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(basestr);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(savepath);
            out.write(bytes);
            out.flush();
            out.close();
        } catch (Exception e) {
        }
    }
}
